package org.ourgrid.acceptance.discoveryservice;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.ourgrid.acceptance.util.discoveryservice.Req_502_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_504_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_505_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_506_Util;
import org.ourgrid.acceptance.util.discoveryservice.Req_507_Util;
import org.ourgrid.discoveryservice.DiscoveryServiceComponent;
import org.ourgrid.reqtrace.ReqTest;

import br.edu.ufcg.lsd.commune.functionaltests.util.TestStub;
import br.edu.ufcg.lsd.commune.identification.ServiceID;

public class Req_507_Test extends DiscoveryServiceAcceptanceTestCase {
	
	Req_502_Util req_502_Util = new Req_502_Util(getComponentContext());
	Req_504_Util req_504_Util = new Req_504_Util(getComponentContext());
	Req_505_Util req_505_Util = new Req_505_Util(getComponentContext());
	Req_506_Util req_506_Util = new Req_506_Util(getComponentContext());
	Req_507_Util req_507_Util = new Req_507_Util(getComponentContext());

	/**
	 * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test, servername = servertest and service = PEER;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer added.
     * Call the getRemoteWorkerProviders message from a client with the following parameters:
          o username = test, servername = servertest;
     * Verify if the result list contains the WorkerProvider added.
	 * @throws Exception
	 */
	@ReqTest(test = "AT-507.1", reqs = "")
	@Test public void test_AT_507_1_consultingARemotePeerAddedToCommunity() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();
		
		//create a WorkerProvider
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
		
		//Call the getRemoteWorkerProviders message
		req_507_Util.getRemoteWorkerProviders(component, dscServiceIDList, "test", "servertest");
	}
	
	/**
	 * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test, servername = servertest and service = PEER;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test2, servername = servertest2 and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test2, servername = servertest2 and service = PEER;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer added.
     * Call the leaveCommunity message with a client with the following attributes:
          o username = test, servername = servertest;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test3, servername = servertest3 and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test3, servername = servertest3 and service = PEER;
     * Call the getRemoteWorkerProviders message from a client with the following parameters:
          o username = test2, servername = servertest2;
     * Verify if the result list contains the WorkerProviders with username = test2 and username = test3
	 * @throws Exception
	 */
	@ReqTest(test = "AT-507.2", reqs = "")
	@Test public void test_AT_507_2_consultingARemotePeerAddedToCommunityAndLaterRemoved() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();

		//create a WorkerProvider
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		//create another WorkerProvider
		TestStub dscTestStub2 = req_505_Util.createDiscoveryServiceClient(new ServiceID("test2", "servertest2", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub2);
		
		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub.getDeploymentID().getContainerID().getUserAtServer());
		dscServiceIDList.add(dscTestStub2.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
		
		//Call leaveCommunity message
		req_506_Util.leaveCommunity(component, dscTestStub);
		
		//create another WorkerProvider
		TestStub dscTestStub3 = req_505_Util.createDiscoveryServiceClient(new ServiceID("test3", "servertest3", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub3);
		
		//Call the getRemoteWorkerProviders message
		dscServiceIDList.clear();
		dscServiceIDList.add(dscTestStub2.getDeploymentID().getContainerID().getUserAtServer());
		dscServiceIDList.add(dscTestStub3.getDeploymentID().getContainerID().getUserAtServer());
		req_507_Util.getRemoteWorkerProviders(component, dscServiceIDList, "test2", "servertest2");
	}
	
	/**
	 * Create a DS;
     * Start a DS with the correct public key;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test, servername = servertest and service = PEER;
     * Call the joinCommunity message with the following parameters:
          o WorkerProvider: username = test, servername = servertest and service = REMOTE_WORKERPROVIDER;
          o PeerStatusProvider: username = test, servername = servertest and service = PEER;
     * Call the getCompleteStatus message;
     * Verify if the conectedPeers list contains the peer with new DeploymentID.
	 * @throws Exception
	 */
	@ReqTest(test = "AT-507.3", reqs = "")
	@Test public void test_AT_507_3_consultingARemotePeerAddedToCommunityAndLaterHasNewDeploymentID() throws Exception{
		//create and start a DS
		DiscoveryServiceComponent component = req_502_Util.startDiscoveryService();

		//create a WorkerProvider
		TestStub dscTestStub = req_505_Util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub);
		
		//create another WorkerProvider
		TestStub dscTestStub2 = req_505_Util.createDiscoveryServiceClient(new ServiceID("test", "servertest", "PEER", "REMOTE_WORKERPROVIDER"));
		
		//Call joinCommunity
		req_505_Util.getRemoteWorkerProviders(component, dscTestStub2, true);

		//Call getCompletStatusMessage
		List<String> dscServiceIDList = new LinkedList<String>();
		dscServiceIDList.add(dscTestStub2.getDeploymentID().getContainerID().getUserAtServer());
		req_504_Util.getCompleteStatus(component, dscServiceIDList);
	}
	
}
